package com.app.push.controller;

import com.alibaba.fastjson.JSONObject;
import com.app.login.service.LoginService;
import com.app.push.Utils.PushTypeEnum;
import com.app.push.android.AndroidBody;
import com.app.push.android.AndroidPayload;
import com.app.push.service.UPushService;
import com.app.sysmodule.reponse.JsonResult;
import com.app.sysmodule.utils.JsonData;
import com.app.sysmodule.utils.PropertyUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 友盟推送
 * Created by song on 2018/1/11.
 */
@Controller
@RequestMapping("/push")
public class UPushController {
    private static Logger log = org.apache.log4j.Logger.getLogger(UPushController.class);
    @Autowired
    private UPushService uPushService;
    @Autowired
    private LoginService loginService;
    @RequestMapping(value = "/sendMessage",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult sendMessage(HttpServletRequest request, HttpServletResponse response){
        JsonResult jsonResult= new JsonResult();
        JSONObject jsonObject = JsonData.getRequestBodyData(request);
        log.info("友盟推送消息接口："+jsonObject);
        if(null != jsonObject){
            List<String> userList = (List<String>)jsonObject.get("userList");
            List<String> companyList = (List<String>)jsonObject.get("companyList");//公司列表
            String type = jsonObject.getString("type");
            String handleType = jsonObject.getString("handleType");
            String source = jsonObject.getString("source");
            String channel = (String)jsonObject.get("channel");//公司id
            String display_type = jsonObject.getString("displayType");
            String filter_type = jsonObject.getString("filterType");//01:通用过滤  02 自定义过滤
            String filter = jsonObject.getString("filter");
            String ticker = jsonObject.getString("ticker");
            String title = jsonObject.getString("title");
            String text = jsonObject.getString("text");
            String userName = jsonObject.getString("userName");
            String password = jsonObject.getString("password");
            try{
                //判断用户名密码
                boolean flag = loginService.verification(userName,password);
                if(flag){
                    if(StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(filter_type) && StringUtils.isNotEmpty(ticker) && StringUtils.isNotEmpty(title) && StringUtils.isNotEmpty(text) && StringUtils.isNotEmpty(handleType)){
                        if(PushTypeEnum.unicast.equals(type)){
//                    uPushService.handleUnicast(appkey,appMasterSecret,token,ticker,title,text);
                        }else if(PushTypeEnum.listcast.equals(type)){
//                    uPushService.handleListcast(users,type,ticker,title,text);
                        }else if(PushTypeEnum.filecast.equals(type)){

                        }else if(PushTypeEnum.broadcast.equals(type)){

                        }else if("groupcast".equals(type)){
                            JSONObject  result = uPushService.handleGroupcast(userList,companyList,type,channel,source,display_type,filter_type,filter,ticker,title,text,handleType);
                            jsonResult.setData(result);
                            jsonResult.setMsg("请求成功");
                            jsonResult.setCode(0);
                        }else if(PushTypeEnum.customizedcast.equals(type)){

                        }
                    }else {
                        jsonResult.setMsg("请求参数有误，请检查请求参数");
                        jsonResult.setData(jsonObject);
                        jsonResult.setCode(1);
                    }

                }else {
                    jsonResult.setMsg("用户名密码错误");
                    jsonResult.setCode(1);
                    return jsonResult;
                }

            }catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage());
                jsonResult.setMsg("推送消息失败,请联系管理员");
                jsonResult.setCode(1);
            }
        }
        return jsonResult;
    }
//    @ResponseBody
//    @RequestMapping(value = "/saveDeviceInfo",method = RequestMethod.POST)
//    public JsonResult saveDeviceInfo(HttpServletRequest request, HttpServletResponse response){
//        JsonResult jsonResult= new JsonResult();
//        JSONObject jsonObject = JsonData.getRequestBodyData(request);
//        if(null != jsonObject){
//            String deviceId = (String)jsonObject.getString("deviceId");
//            String userId = (String)jsonObject.getString("userId");
//            String deviceToken = (String)jsonObject.getString("deviceToken");
//            String channel = (String)jsonObject.getString("channel");
//            String source = (String)jsonObject.getString("source");
//            String type = (String)jsonObject.getString("type");
//            if(StringUtils.isNotEmpty(deviceId) && StringUtils.isNotEmpty(userId) && StringUtils.isNotEmpty(deviceToken) && StringUtils.isNotEmpty(channel) && StringUtils.isNotEmpty(source) && StringUtils.isNotEmpty(type)){
//                try{
//                    boolean result = uPushService.saveDeviceInfo(deviceId,userId,deviceToken,channel,source,type);
//                    if(result){
//                        jsonResult.setCode(0);
//                        jsonResult.setMsg("请求成功");
//                    }else {
//                        jsonResult.setCode(1);
//                        jsonResult.setMsg("请求失败");
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                    log.error(e.getMessage());
//                    jsonResult.setCode(1);
//                    jsonResult.setMsg("请求失败");
//                }
//            }else {
//                jsonResult.setCode(1);
//                jsonResult.setMsg("请求参数有误");
//                return jsonResult;
//            }
//        }else {
//            jsonResult.setCode(1);
//            jsonResult.setMsg("请求参数有误");
//            return jsonResult;
//        }
//        return jsonResult;
//    }
}
